import 'package:bloc_pattern/bloc_pattern.dart';
import 'package:flutter/material.dart';
import 'package:connectivity/connectivity.dart';

import 'package:fluro/fluro.dart';
import 'package:event_bus/event_bus.dart';
import 'package:fluttertoast/fluttertoast.dart';

import 'package:logging/logging.dart';
import 'package:xxx_app/src/blocs/increment/increment_bloc.dart';

import 'main_tabbar.dart';
import 'routes.dart';
import 'config/application.dart';
import 'config/theme.dart';

import 'screens/splash/splash_screen.dart';

final _logger = new Logger('app');

class XxxApp extends StatefulWidget {

  @override
  _XxxAppState createState() => _XxxAppState();
}

class _XxxAppState extends State<XxxApp> with WidgetsBindingObserver {
  dynamic subscription;

  _XxxAppState() {

    final router = new Router();
    AppRoutes.configureRoutes(router);
    Application.router =router;

  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    WidgetsBinding.instance.addObserver(this);
    
    Application.eventBus = EventBus();

    initListener();

  }

  initListener() {
    subscription = new Connectivity().onConnectivityChanged.listen((ConnectivityResult result ) {
      String networkResult = "";
      
      switch ( result ) {
        case ConnectivityResult.mobile:
          networkResult = "当前处于移动网络";
          break;
        case ConnectivityResult.wifi:
          networkResult = "当前处于wifi网络";
          break;
        case ConnectivityResult.none:
          networkResult = "当前没有网络连接！";
          break;
        default: break;

      }
      
      Fluttertoast.showToast(
        msg: networkResult,
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.TOP,
        timeInSecForIos: 1,
        backgroundColor: Colors.black,
        textColor: Colors.white,
        fontSize: 16.0,
      );

    });


  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    // TODO: implement didChangeAppLifecycleState
    
    _logger.finest(" -------- == == == == -- -- -- $state ");

    switch (state) {
      case AppLifecycleState.inactive:

        break;
      case AppLifecycleState.paused:


        break;
      case AppLifecycleState.resumed:


        break;
      case AppLifecycleState.suspending:


        break;
      default:
        break;
    }

    super.didChangeAppLifecycleState(state);
  }

  @override
  Widget build(BuildContext context) {
    final app = BlocProviderList(
      child: MaterialApp(
        title: "Zlito",
        theme: AppTheme.themeData,
        home: SplashScreen(),
        onGenerateRoute: Application.router.generator,

      ),
      listBloc: [
        Bloc(IncrementBloc()),
        
      ],
    );

    // return app;
    
    // final app = MaterialApp(
    //   title: "Zlito",
    //   theme: AppTheme.themeData,
    //   // initialRoute: "/",
    //   // ThemeData(
    //   //   primaryColor: Colors.deepPurple,
    //   // ),
    //   // home: MainTabbar(),
    //   home: SplashScreen(),
    //   // localizationsDelegates: [ ],
    //   onGenerateRoute: Application.router.generator,

    // );
    _logger.finest(" ---- == initial route = ${app.child}" );
    
    return app;

  }

  @override
  void dispose() {
    // TODO: implement dispose
    WidgetsBinding.instance.removeObserver(this);

    super.dispose();
  }


}









